package com.sspku.weinotes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sspku.weinotes.entity.NoteBook;
import com.sspku.weinotes.entity.User;
import com.sspku.weinotes.exception.IncorrectParameterException;
import com.sspku.weinotes.exception.NoSuchIdException;
import com.sspku.weinotes.mapper.UserMapper;
import com.sspku.weinotes.service.NoteBookService;
import com.sspku.weinotes.mapper.NoteBookMapper;
import com.sspku.weinotes.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
* @author user
* @description 针对表【note_book】的数据库操作Service实现
*/
@Service
public class NoteBookServiceImpl extends ServiceImpl<NoteBookMapper, NoteBook>
    implements NoteBookService{
    private static final Logger logger = LoggerFactory.getLogger(NoteBookServiceImpl.class);
    @Autowired
    private NoteBookMapper noteBookMapper;
    @Autowired
    private NoteService noteService;
    @Autowired
     private UserMapper userMapper;

    @Override
    public List<NoteBook> getAllNoteBook(int uid) throws NoSuchIdException {
        //先检查Uid是否存在
        User user=userMapper.selectById(uid);
        if(user==null) throw new NoSuchIdException("用户Id不存在");
        LambdaQueryWrapper<NoteBook> lqw = new LambdaQueryWrapper<NoteBook>();
        lqw.eq(NoteBook::getUid, uid);
        return noteBookMapper.selectList(lqw);
    }

    @Override
    public void deleteNoteBookById(int bid) throws NoSuchIdException {
        NoteBook noteBook = noteBookMapper.selectById(bid);
        if (noteBook == null) {
            logger.info("the note book id " + bid + " does not exist");
            throw new NoSuchIdException("笔记本Id:" +bid + " 不存在");
        }
        noteService.deleteByNoteBookId(bid);
        noteBookMapper.deleteById(bid);
    }

    @Override
    public boolean modifyNoteBookName(int id, String name) throws NoSuchIdException, IncorrectParameterException {
        if (StringUtils.isBlank(name)) {
            throw new IncorrectParameterException("参数不正确:笔记本名不能为空" );
        }
        NoteBook noteBook = noteBookMapper.selectById(id);
        if (noteBook == null) {
            logger.info("the note book id " + id + " does not exist");
            throw new NoSuchIdException("笔记本Id:" + id + " 不存在");
        }
        noteBook.setBname(name);
        noteBook.setGmtModified(new Date());
        return noteBookMapper.updateById(noteBook)>0;
    }

    @Override
    public boolean addNoteBook(int uid, String name) throws IncorrectParameterException {
        if (StringUtils.isBlank(name)) {
            throw new IncorrectParameterException("参数不正确:笔记本名不能为空" );
        }
        //int uid = SecurityUtils.getUser();
        NoteBook noteBook = new NoteBook();
        noteBook.setGmtCreate(new Date());
        noteBook.setGmtModified(new Date());
        noteBook.setBname(name);
        noteBook.setUid(uid);
        return noteBookMapper.insert(noteBook)>0;
    }
}




